﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ozone.Help
{
    public class RedisHelper
    {

        public object objLock { get; set; } = new object();

        //AGVNowPoint     AGV小车当前点位
        //AGVToPoint      AGV小车 去往点位
        //AGVRuningState   AGV小车运行状态
        //RobotExecuteActionNumber    机械臂执行动作编号
        //RobotRuningState    机械臂运行状态

        private readonly RedisClient _redisClient;

        public RedisHelper(string host = "192.168.0.108", int port = 6379, string password = "password01!")
        {
            _redisClient = new RedisClient(host, port, password);
        }

        // 设置键值
        public void SetValue(string key, string value)
        {
            bool lockTaken = false;
            try
            {
                Monitor.TryEnter(objLock, 5000, ref lockTaken);
                if (lockTaken)
                {


                    if (string.IsNullOrEmpty(key))
                        throw new ArgumentException("Key cannot be null or empty", nameof(key));

                    _redisClient.Set(key, value);


                }

                return;
            }
            catch (Exception)
            {
                return;
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(objLock);
                }
            }
        }




        // 获取键值
        public string GetValue(string key)
        {
            bool lockTaken = false;
            try
            {
                Monitor.TryEnter(objLock, 5000, ref lockTaken);
                if (lockTaken)
                {


                    if (string.IsNullOrEmpty(key))
                        throw new ArgumentException("Key cannot be null or empty", nameof(key));

                    return _redisClient.Get<string>(key);


                }

                return null;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(objLock);
                }
            }
        }



        /// <summary>
        /// 判断Key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool KeyExists(string key)
        {
            bool lockTaken = false;
            try
            {
                Monitor.TryEnter(objLock, 5000, ref lockTaken);
                if (lockTaken)
                {
                    if (string.IsNullOrEmpty(key))
                        throw new ArgumentException("Key cannot be null or empty", nameof(key));

                    return _redisClient.Exists(key) > 0;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(objLock);
                }
            }
        }



    }
}
